package com.hss.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hss.base.interfaces.IBaseView;
import com.hss.base.util.StartActivityUtil;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/2/10.
 */

public abstract class BaseFragment extends Fragment implements IBaseView{
    MaterialDialog lodingDilog= null;//loading dialog
    boolean isShowing = false;// 判断fragment 是否显示

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //与viewpager 使用时可判断是否显示
//        isShow = isVisibleToUser;
    }

    @Override
    public void onResume() {
        super.onResume();
        isShowing = true;
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    //判断Fragment是显示还是隐藏，但是第一次显示要在onResume()里判断。
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShowing = !hidden;
    }

    @Override
    public void showLoadingDialog() {
        if (!isShowing || getActivity() == null || getActivity().isFinishing()){
            return;
        }
        lodingDilog =   new MaterialDialog.Builder(getActivity())
                .content(R.string.loading)
                .progress(true, 0)
                .show();
    }

    @Override
    public void hideLoadingDialog() {
            if (lodingDilog !=null){
                lodingDilog.dismiss();
            }
    }



    @Override
    public void showToast(String text) {
        if (getActivity() ==null || getActivity().isFinishing()){
            return;
        }
        Toast.makeText(getActivity().getApplicationContext(),text,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showToast(int textId) {
        if (getActivity() ==null || getActivity().isFinishing()){
            return;
        }
        Toast.makeText(getActivity().getApplicationContext(),textId,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void destoryRes() {

    }


    @Override
    public <T extends AppCompatActivity> void startActy(Class<T> clazz) {
        StartActivityUtil.startActivity(clazz, this);
    }

    @Override
    public <T extends AppCompatActivity> void startActy(Class<T> clazz, Bundle data) {
        StartActivityUtil.startActivity(clazz,data, this);

    }

    @Override
    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, Bundle data, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz,requestCode,data, this);

    }

    @Override
    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz,requestCode, this);

    }


    @Override
    public void finishActivity() {
        getActivity().finish();
    }

    @Override
    public void finishActivity(int resultCode) {
        getActivity().setResult(resultCode);
        getActivity().finish();
    }

    @Override
    public void finishActivity(int resultCode, Intent data) {
        getActivity().setResult(resultCode,data);
        getActivity().finish();
    }
    private CompositeSubscription mCompositeSubscription;

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }

    }


    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }


    public void clearSubscription(){
        if (mCompositeSubscription != null ) {
            mCompositeSubscription.clear();
        }
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        onUnsubscribe();

        clearSubscription();
        destoryRes();
    }

    public void initViews(){

    }
    public void initData(){

    }

    public void initListener(){
    }
}
